package kr.ac.chungbuk;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class BoardController {
	ArrayList<BoardDTO> boardList = new ArrayList<BoardDTO>();
	IBoardDAO ibaties = new IBoardDAO();
	@RequestMapping("index")
	public String index(){
		return "index";
	}
	/*@RequestMapping("writeProc")
	public ModelAndView writeProc(ModelAndView mav,String title,String content){
		BoardDTO boardDTO = new BoardDTO();
		boardDTO.setTitle(title);
		boardDTO.setContent(content);
		boardList.add(boardDTO);
		ibaties.insert(boardDTO);
		mav.addObject("boardList", boardList);
		mav.setViewName("writeProc");
		return mav;		
	}*/
	@RequestMapping(value="insertFormProc",method=RequestMethod.GET)
	public ModelAndView insertFormProc(HttpServletRequest request){
		try {
			request.setCharacterEncoding("euc-kr");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ModelAndView mav = new ModelAndView();
		String title = request.getParameter("title");
		String content=request.getParameter("content");
		System.out.println("title="+title);
		System.out.println("content="+content);
		BoardDTO boardDTO=new BoardDTO(title, content);
		System.out.println("content : "+boardDTO.getContent());
		boardList.add(boardDTO);
		mav.addObject("boardList", boardList);
		ibaties.insert(boardDTO);
		mav.setViewName("redirect:list");
		return mav;
	}
	@RequestMapping("list")
	public ModelAndView listBoard(ModelAndView mav){
		List list=ibaties.selectAll();
		mav.addObject("boardList",list);
		mav.setViewName("list");
		mav.addObject("bodypage","list.jsp");
		return mav;
	}
	@RequestMapping(value="delete",method=RequestMethod.GET)
	public String delete(String title){
		ibaties.delete(title);
		return "redirect:list";
	}
	@RequestMapping("updateForm")
	public ModelAndView updateForm(String title,ModelAndView mav){
		BoardDTO board = ibaties.selectOne(title);
		mav.addObject("board",board);
		mav.setViewName("updateForm");
		return mav;
	}
	@RequestMapping("updateFormProc")
	public String updateForm(BoardDTO board){
		ibaties.update(board);
		return "redirect:list";
		
	}
	
}